﻿using movie.tool.module.film.model;
using movie.tool.module.http;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace movie.tool.module.film.ui
{
    /// <summary>
    /// 剧本信息控件
    /// </summary>
    public partial class MovieInfoControl : UserControl, INotifyPropertyChanged
    {
        private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        public event PropertyChangedEventHandler PropertyChanged;
        public event Action<string[]> MovieTagsChanged;
        public event Action NotifyMovieTagProperty;

        /// <summary>
        /// 发送通知消息
        /// </summary>
        /// <param name="propetyName">属性名称字符串</param>
        protected void SendPropertyChanged(string propetyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propetyName));
            }
        }

        private List<MovieTag> movieTags;
        private ObservableCollection<MovieTag> movieMaterTags;
        public List<MovieTag> MovieTags
        {
            get => movieTags;
            private set
            {
                movieTags = value;
                SendPropertyChanged(nameof(MovieTags));
            }
        }
        public ObservableCollection<MovieTag> MovieMasterTags
        {
            get => movieMaterTags;
            private set
            {
                movieMaterTags = value;
                SendPropertyChanged(nameof(movieMaterTags));
            }
        }
        public MovieInfoControl()
        {
            InitializeComponent();
            MovieTags = new List<MovieTag>();
            MovieMasterTags = new ObservableCollection<MovieTag>();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if(MovieTagsChanged != null)
            {
                MovieTagsChanged.Invoke(MovieTags.Where(item => item.IsChecked).Select(item => item.Name).ToArray());
            }
        }

        private void CheckMasterBox_Checked(object sender, RoutedEventArgs e)
        {
            if (MovieTagsChanged != null)
            {
                MovieTagsChanged.Invoke(MovieTags.Where(item => item.IsChecked).Select(item => item.Name).ToArray());
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if(MovieTagsChanged != null)
            {
                MovieTagsChanged.Invoke(MovieTags.Where(item => item.IsChecked).Select(item => item.Name).ToArray());
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox c = sender as ComboBox;
            if (c != null)
            {
                c.SelectedItem = null;
            }
            Dispatcher.InvokeAsync(() =>
            {
                if(NotifyMovieTagProperty != null)
                {
                    NotifyMovieTagProperty.Invoke();
                }
            });
            e.Handled = true;
        }

        private async void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            MovieTags.Clear();
            try
            {
                List<MovieTag> res = await HttpInvoker.GetAsync<List<MovieTag>>("movieserver/movieTags");
                if (res != null && res.Count > 0)
                {
                    res.ForEach(item => MovieTags.Add(item));
                }
            }
            catch(Exception ex)
            {
                logger.Error(ex, "获取标签列表失败");
            }
            foreach(MovieTag Tag in MovieTags)
                {
                    if (Tag.Name=="剧情")
                    {
                        MovieMasterTags.Add(Tag);
                    }
                }
            
        }
    }
}
